/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import doa.MessageDAO;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import models.Message;
import models.Twitterer;
import utils.JSFSessionHandler;

/**
 *
 * @author Misja
 */
public class MessageBean implements Serializable {

    private Message message;

    public MessageBean() {
        this.message = new Message();
    }

    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    public void saveMessage() {
        Twitterer owner = (Twitterer) JSFSessionHandler.getObjectFromSession("currentUser");
        if (owner.isCanPlace()) {
            this.message.setOwner(owner);
            boolean result = MessageDAO.saveMessage(this.message);
            if (result) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Message is succesfully added", null));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Message couldnt be saved", null));
            }
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Message not created due to insufficient rights", null));
        }
    }

    public String toTwittererOverView() {
        return "/TwittererOverview.xhtml?faces-redirect=true";
    }
}
